package com.neusoft.elmboot.service.impl;

import com.neusoft.elmboot.mapper.UserMapper;
import com.neusoft.elmboot.po.User;
import com.neusoft.elmboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserSeriveImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
public User getUserByIdByPass(User user) {
    User storedUser = userMapper.getUserById(user.getUserId());
    
    // 如果用户存在，并且密码匹配
    if (storedUser != null) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        if (passwordEncoder.matches(user.getPassword(), storedUser.getPassword())) {
            return storedUser;
        }
    }
    
    // 如果用户不存在或密码不匹配，返回null或自定义处理
    return null;
}


    @Override
    public User getUserById(String userId) {
        return userMapper.getUserById(userId);
    }

    @Override
    public int saveUser(User user) {
        // 创建密码加密器
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

        // 对用户的密码进行加密
        String encryptedPassword = passwordEncoder.encode(user.getPassword());

        // 将加密后的密码设置回 User 对象
        user.setPassword(encryptedPassword);

        // 保存用户到数据库
        return userMapper.saveUser(user);
    }

    @Override
    public int updateUserById(User user) {
        return userMapper.updateUserById(user);
    }

    @Override
    public int removeUser(User user) {
        userMapper.removeCart(user);
        userMapper.removeOrders(user);
        userMapper.removeComments(user);
        userMapper.removeDeliveryaddress(user);
        return userMapper.removeUser(user);
    }
}
